package com.ruoyi.web.controller.chargepile;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.chargepile.domain.Points;
import com.ruoyi.chargepile.domain.PointsRecords;
import com.ruoyi.chargepile.domain.enums.RolesEnum;
import com.ruoyi.chargepile.service.PointsRecordsService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.util.DateRange;
import com.ruoyi.util.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 积分记录表 前端控制器
 * </p>
 *
 * @author zxc
 * @since 2024-07-03
 */
@RestController
@RequestMapping("/points-records")
public class PointsRecordsController {

    @Autowired
    private PointsRecordsService pointsRecordsService;

    @Autowired
    private ISysRoleService iSysRoleService;

    @GetMapping("/listPointsRecords")
    public AjaxResult listPointsRecords(Pages pages, DateRange dateRange, PointsRecords pointsRecords) {
        PageHelper.startPage(pages.getPageNum(), pages.getPageSize());
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();

        LambdaQueryWrapper<PointsRecords> pointsLambdaQueryWrapper = new LambdaQueryWrapper<>();

        pointsLambdaQueryWrapper
                .eq(PointsRecords::getUserId, sysUser.getUserId())
                .eq(StringUtils.isNotEmpty(pointsRecords.getPointsType()), PointsRecords::getPointsType, pointsRecords.getPointsType())
                .between(dateRange.getBeginTime() != null, PointsRecords::getCreatedTime, dateRange.getBeginTime(), dateRange.getEndTime())
                .orderByDesc(PointsRecords::getId);



        List<PointsRecords> list = pointsRecordsService.list(pointsLambdaQueryWrapper);

        return AjaxResult.success(new PageInfo<PointsRecords>(list));
    }

}

